﻿using NDK.Frm;
using NDK.Module.BLL;
using NDK.Module.Model;
using NDK.Motion.BLL;
using NDK.Motion.MachineManager;
using NDK.Motion.Monitor;
using System;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Threading;

namespace NDK.Module.StationLogic
{
    [Monitor(Description = "上料平台1")]
    public class LoadingTrans1 : StationLogic
    {
        #region 通用
        //记录当前步骤，用于步骤变化的比较
        private StationStep _curStep = StationStep.开始准备;
        //对外提供工位的步骤变化事件
        public override event PropertyChangedEventHandler PropertyChanged;
        //对外提供唯一实例
        public static LoadingTrans1 Instance = new LoadingTrans1();
        //私有化的构造函数
        private LoadingTrans1 () : base()
        {
            this.Name = "上料平台1";
        }
        //步骤字段、属性
        private StationStep _step = StationStep.开始准备;
        [Monitor(Description = "当前步骤")]
        public StationStep Step
        {
            get
            {
                return _step;
            }
            set
            {
                if(_curStep != value)
                {
                    _step = value;
                    _curStep = value;

                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(value.ToString()));
                }
            }
        }
        #endregion

        /// <summary>
        /// 工站初始化
        /// </summary>
        public override void Init ()
        {
            base.Init();
            Step = StationStep.开始准备;
            logBaseInfo = "LoadingTrans1";
        }

        private bool CylinderHome ()
        {
            return CylinderMethod(false, (Machine.cy_双动子平台1气缸1, !Common.屏蔽治具1), (Machine.cy_双动子平台1气缸2, !Common.屏蔽治具2));
        }

        private bool CylinderWork ()
        {
            return CylinderMethod(true, (Machine.cy_双动子平台1气缸1, !Common.屏蔽治具1), (Machine.cy_双动子平台1气缸2, !Common.屏蔽治具2));
        }

        private bool VacuumOn ()
        {
            return VacuumMethod(true, (Machine.do_上料平台1真空1吸, Machine.di_上料平台1真空1反馈, !Common.屏蔽治具1), (Machine.do_上料平台1真空2吸, Machine.di_上料平台1真空2反馈, !Common.屏蔽治具2));
        }

        public void VacuumOff ()
        {
            Machine.do_上料平台1真空1吸.OFF();
            Machine.do_上料平台1真空2吸.OFF();
            Machine.do_上料平台1真空1破.ON();
            Machine.do_上料平台1真空2破.ON();
            Thread.Sleep(150);
            Machine.do_上料平台1真空1破.OFF();
            Machine.do_上料平台1真空2破.OFF();
        }

        /// <summary>
        /// 工站动作流程
        /// </summary>
        /// <param name="mode"></param>
        public override void ActionProcess ()
        {
            try
            {
                switch(Step)
                {
                    case StationStep.开始准备:
                        Step = StationStep.流程开始;
                        break;

                    case StationStep.流程开始:
                        if(Common.isWriteLog)
                        {
                            logCtStr = string.Empty;
                            swStepTime.Restart();
                            swStationCT.Restart();
                            logCtStr += $"{logBaseInfo},LoadingTrans1工站开始,{swStepTime.ElapsedMilliseconds},{Environment.NewLine}";
                        }
                        Step = StationStep.轴初始位;
                        break;

                    case StationStep.轴初始位:
                        if(MotionControl.AxesMove(Common.Line == "MUR" ? Machine.pos_DoubleMotor_Panel1_初始位 : Machine.pos_DoubleMotor_Panel1_取料位))
                        {
                            工位要料 = true;
                            Step = StationStep.等待上工位完成;
                        }
                        break;

                    case StationStep.等待上工位完成:
                        if(LoadingMove.Instance.工位完成)
                        {
                            Common.sw4.Restart();
                            Step = Common.Line == "MUR" ? StationStep.轴取料位 : StationStep.下压取料;
                        }
                        break;

                    case StationStep.轴取料位:
                        if(MotionControl.AxesMove(Machine.pos_DoubleMotor_Panel1_取料位))
                        {
                            Step = StationStep.下压取料;
                        }
                        break;

                    case StationStep.下压取料:
                        if(CylinderWork())
                        {
                            LoadingMove.Instance.VacummOff();
                            Step = StationStep.打开真空;
                        }
                        break;

                    case StationStep.打开真空:
                        if(VacuumOn())
                        {
                            Step = StationStep.抬起取料;
                        }
                        break;

                    case StationStep.抬起取料:
                        if(CylinderHome())
                        {
                            Step = StationStep.真空判断;
                        }
                        break;

                    case StationStep.真空判断:
                        if(VacuumOn())
                        {
                            工位要料 = false;
                            Step = StationStep.克隆数据;
                        }
                        break;

                    case StationStep.克隆数据:
                        Step = StationStep.等待下工位开要料;
                        break;

                    case StationStep.等待下工位开要料:
                        if(PPG.Instance.工位要料)
                        {
                            Step = StationStep.轴放料位;
                        }
                        break;

                    case StationStep.轴放料位:
                        if(MotionControl.AxesMove(Machine.pos_DoubleMotor_Panel1_放料位))
                        {
                            Step = StationStep.下压放料;
                        }
                        break;

                    case StationStep.下压放料:
                        if(CylinderWork())
                        {
                            VacuumOff();
                            Step = StationStep.抬起放料;
                        }
                        break;

                    case StationStep.抬起放料:
                        if(CylinderHome())
                        {
                            Step = StationStep.轴避让位;
                        }
                        break;

                    case StationStep.轴避让位:
                        if(MotionControl.AxesMove(Machine.pos_DoubleMotor_Panel1_取料位))
                        {
                            工位完成 = true;
                            Step = StationStep.等待下工位关要料;
                        }
                        break;

                    case StationStep.等待下工位关要料:
                        if(!PPG.Instance.工位要料)
                        {
                            工位完成 = false;
                            Step = StationStep.判断模式;
                        }
                        break;

                    case StationStep.判断模式:
                        if((!Common.GRR循环模式 || Common.GRREnd) && !Common.pointCheck)
                        {
                            Step = StationStep.流程结束;
                        }
                        else
                        {
                            GRR工位要料 = true;
                            Step = StationStep.GRR等待下工位完成;
                        }
                        break;

                    case StationStep.GRR等待下工位完成:
                        if(PPG.Instance.GRR工位完成)
                        {
                            Step = StationStep.GRR轴PPG位;
                        }
                        break;

                    case StationStep.GRR轴PPG位:
                        if(MotionControl.AxesMove(Machine.pos_DoubleMotor_Panel1_放料位))
                        {
                            Step = StationStep.GRR下压取料;
                        }
                        break;

                    case StationStep.GRR下压取料:
                        if(CylinderWork())
                        {
                            Step = StationStep.GRR打开真空;
                        }
                        break;

                    case StationStep.GRR打开真空:
                        if(VacuumOn())
                        {
                            Step = StationStep.GRR取料抬起;
                        }
                        break;

                    case StationStep.GRR取料抬起:
                        if(CylinderHome())
                        {
                            Step = StationStep.GRR真空判断;
                        }
                        break;

                    case StationStep.GRR真空判断:
                        if(VacuumOn())
                        {
                            Step = StationStep.GRR轴放料位;
                        }
                        break;

                    case StationStep.GRR轴放料位:
                        if(MotionControl.AxesMove(Machine.pos_DoubleMotor_Panel1_取料位))
                        {
                            GRR工位要料 = false;
                            Step = Common.GRR换穴 == 1 ? StationStep.GRR穴1放穴2 : StationStep.GRR等待上工位开要料;
                        }
                        break;

                    case StationStep.GRR穴1放穴2:
                        if(!Common.屏蔽治具1)
                        {
                            if(MotionControl.AxesMove(Machine.pos_DoubleMotor_Panel1_穴1放穴2))
                            {
                                CylinderMethod(true, (Machine.cy_双动子平台1气缸1, !Common.屏蔽治具1));
                                Machine.do_上料平台1真空1吸.OFF();
                                Machine.do_上料平台1真空1破.ON();
                                Thread.Sleep(150);
                                Machine.do_上料平台1真空1破.OFF();
                                CylinderMethod(false, (Machine.cy_双动子平台1气缸1, !Common.屏蔽治具1));
                                Step = StationStep.GRR穴2放穴1;
                            }
                        }
                        else
                        {
                            Step = StationStep.GRR穴2放穴1;
                        }
                        break;

                    case StationStep.GRR穴2放穴1:
                        if(!Common.屏蔽治具2)
                        {
                            if(MotionControl.AxesMove(Machine.pos_DoubleMotor_Panel1_穴2放穴1))
                            {
                                CylinderMethod(true, (Machine.cy_双动子平台1气缸2, !Common.屏蔽治具2));
                                Machine.do_上料平台1真空2吸.OFF();
                                Machine.do_上料平台1真空2破.ON();
                                Thread.Sleep(150);
                                Machine.do_上料平台1真空2破.OFF();
                                CylinderMethod(false, (Machine.cy_双动子平台1气缸2, !Common.屏蔽治具2));
                                Step = StationStep.物料交换完成;
                            }
                        }
                        else
                        {
                            Step = StationStep.物料交换完成;
                        }
                        break;

                    case StationStep.物料交换完成:
                        Common.GRR换穴 = 2;
                        GRR工位完成 = true;
                        if(Common.屏蔽治具1 || Common.屏蔽治具2)
                        {
                            Common.屏蔽治具1 = !Common.屏蔽治具1;
                            Common.屏蔽治具2 = !Common.屏蔽治具2;
                            
                            //Frm_Setting.Instance.ParamsBoolDataTable.AsEnumerable().SingleOrDefault(r => (string)r["参数名称"] == "屏蔽治具1")[2] = Common.屏蔽治具1.ToString();
                            //Frm_Setting.Instance.ParamsBoolDataTable.AsEnumerable().SingleOrDefault(r => (string)r["参数名称"] == "屏蔽治具2")[2] = Common.屏蔽治具2.ToString();
                            //Frm_Setting.Instance.ParamsBoolDataAdapter.Update(Frm_Setting.Instance.ParamsBoolDataTable);
                        }
                        Step = StationStep.GRR等待上工位关要料;
                        break;

                    case StationStep.GRR等待上工位开要料:
                        if(LoadingMove.Instance.GRR工位要料)
                        {
                            Step = StationStep.GRR下压放料;
                        }
                        break;

                    case StationStep.GRR下压放料:
                        if(CylinderWork())
                        {
                            GRR工位完成 = true;
                            Step = StationStep.GRR等待上工位关要料;
                        }
                        break;

                    case StationStep.GRR等待上工位关要料:
                        if(!LoadingMove.Instance.GRR工位要料)
                        {
                            GRR工位完成 = false;
                            Step = StationStep.GRR抬升离开;
                        }
                        break;

                    case StationStep.GRR抬升离开:
                        if(CylinderHome())
                        {
                            Step = StationStep.流程结束;
                        }
                        break;

                    case StationStep.流程结束:
                        Common.sw4.Stop();
                        if(Common.isWriteLog)
                        {
                            logCtStr += $"{logBaseInfo},LoadingTrans1结束,{swStepTime.ElapsedMilliseconds},{Environment.NewLine}";
                            logCtStr += $"{logBaseInfo},LoadingTrans1结束,{swStationCT.ElapsedMilliseconds},{Environment.NewLine}";
                            PublicMethod.WriteLogToTxt(logCtStr, $"000StepCT_{DateTime.Now.ToString("yyyyMMdd")}", "4.LoadingTrans1");
                        }
                        Step = StationStep.开始准备;
                        break;
                    default:
                        break;

                }
            }
            catch(Exception ex)
            {
                UILog_Error($"[LoadingTrans1 ActionProcess Thread ERROR] {ex.Message}");
            }
        }

        public override void EmptyActionProcess ()
        {
            ActionProcess();
        }

        public override void GRRActionProcess ()
        {
            ActionProcess();
        }

        /// <summary>
        /// 工位步骤定义
        /// </summary>
        public enum StationStep
        {
            开始准备,
            流程开始,
            轴初始位,
            等待上工位完成,
            轴取料位,
            下压取料,
            取料到位,
            打开真空,
            抬起取料,
            真空判断,
            克隆数据,
            等待下工位开要料,
            轴放料位,
            下压放料,
            关闭真空,
            抬起放料,
            轴避让位,
            等待下工位关要料,
            判断模式,

            GRR等待下工位完成,
            GRR轴PPG位,
            GRR下压取料,
            GRR打开真空,
            GRR取料抬起,
            GRR真空判断,
            GRR轴放料位,
            GRR等待上工位开要料,
            GRR下压放料,
            GRR等待上工位关要料,
            GRR放料到位,
            GRR等待离开,
            GRR关闭真空,
            GRR抬升离开,
            GRR放料完成,

            GRR穴1放穴2,
            GRR穴2放穴1,
            物料交换完成,
            流程结束,
        }

    }
}
